<template>
	<view>
		<button v-if="is_style=='button'" class="flex j-c a-c b_y cff but_login" style="background-color: #00A0E9;"
			@getAuthorize="on_login" scope="userInfo" open-type="getAuthorize">
			<text class="f30">支付宝授权登录</text>
		</button>
		<view v-if="is_style=='img'">
			<button @getAuthorize="on_login" scope="userInfo" open-type="getAuthorize">
				<image class="q_y login_icon" src="@/static/icon/ali.png">
				</image>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: "login-mp-ali",
		props: {
			userName: {
				type: String,
				default: ''
			},
			avatarUrl: {
				type: String,
				default: ''
			},
			agr: {
				type: Boolean,
				default: false
			},
			agr: {
				type: Boolean,
				default: false
			},
			is_style: {
				type: String,
				default: 'button'
			},
		},
		data() {
			return {}
		},
		created() {
			this.face = this.avatarUrl;
			this.nickname = this.userName;
		},
		methods: {
			on_login() {
				let that = this;
				if (!this.agr) {
					uni.showToast({
						title: '请勾选同意协议',
						icon: "none"
					});
					return;
				}

				//支付宝小程序授权登录
			 
				my.getOpenUserInfo({
					fail: (res) => {},
					success: (res) => {
						uni.showLoading({
							title: '正在登录中...',
							mask: true
						})
						let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response
						// userInfo = {
						// 	"code": "10000",
						// 	"msg": "Success",
						// 	"avatar": "https://tfs.alipayobjects.com/images/partner/AT027QT6S7rroAAAAAAAAAAAAADtl2AA",
						// 	"city": "",
						// 	"countryCode": "",
						// 	"gender": "",
						// 	"nickName": "333",
						// 	"province": ""
						// }
						that.nickname = userInfo.nickName;
						that.face = userInfo.avatar;
						my.getAuthCode({
							scopes: 'auth_base',
							success: (res) => {
								//在此处将res.authCode发送到后端 
								let code = res.authCode;
								let requestParams = {
									face: that.face,
									nickname: that.nickname,
									ali_code: code,
									sex:0,
									tel: '',
									checknum: '',
								};
								that.Banmikeji.ali_login(requestParams).then(ret => {
									that.$emit('confirm', ret);
								})
							}
						})


					},
				});
			},

		}
	}
</script>
<style lang="scss">
	.but_login {
		width: 600rpx;
		height: 80rpx;
		background-color: #1DC069;
	}
	button::after {
		border: none;
	}
	
	button {
		 
		border: 0; padding: 0;
		background-color: transparent;
	
	}
</style>